package oct2013;

public class RotateImage {
	public void rotate(int[][] matrix) {
		int n = matrix.length;
		for (int i = 0; i < n / 2; ++i) {
			for (int j = 0; j < n; ++j) {
				swap(matrix, j, i, j, n - i - 1);
			}
		}
		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < n - i; ++j) {
				swap(matrix, i, j, n - j - 1, n - i - 1);
			}
		}
	}

	void swap(int[][] matrix, int x1, int y1, int x2, int y2) {
		if (matrix[x1][y1] == matrix[x2][y2])
			return;
		matrix[x1][y1] ^= matrix[x2][y2];
		matrix[x2][y2] ^= matrix[x1][y1];
		matrix[x1][y1] ^= matrix[x2][y2];
	}
}
